package com.cbw.wx.controller;

import com.cbw.bean.BootStrapTableExpress;
import com.cbw.bean.Express;
import com.cbw.bean.Message;
import com.cbw.bean.User;
import com.cbw.mvc.ResponseBody;
import com.cbw.service.ExpressService;
import com.cbw.util.DateFormatUtil;
import com.cbw.util.JSONUtil;
import com.cbw.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;


public class ExpressController {
    /**
     * 微信端，通过手机查询所有快递
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/findExpressByUserPhone.do")
    public String findByUserPhone(HttpServletRequest request, HttpServletResponse response){
        System.out.println("findExpressByUserPhone.do被执行");
        //从session中获取当前登录用户wxUser
        User wxUser = UserUtil.getWxUser(request.getSession());
        //从用户中获取手机号
        String userPhone = wxUser.getUserPhone();
        //调用ExpressService中的findByUserPhone方法获取该手机号的所有快递(已取件/未取件)
        List<Express> list = ExpressService.findByUserPhone(userPhone);
        //修改快递的出库时间、快递状态以及取件码的形态
        ArrayList<BootStrapTableExpress> list1 = new ArrayList<>();
        for (Express e : list) {
            String inTime = DateFormatUtil.format(e.getInTime());//将TimeStamp类型转成string
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status=e.getStatus()==0?"待取件":"已取件";
            String code=e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list1.add(e2);
        }
        //消息对象
        Message msg = new Message();
        //判断快递是否存在
        if (list.size()==0){
            //该手机号没有对应的快递
            msg.setStatus(-1);
        }else {
            msg.setStatus(0); //该手机号有对应的快递
            //使用jdk8新技术stream流处理(遍历)集合，获取流，然后根据已取件/未取件进行过滤，并按照时间排序，返回流对象
            //遍历集合，将所有未取件快递存入流中
            Stream<BootStrapTableExpress> status0Express = list1.stream().filter(express -> {
                if (express.getStatus().equals("待取件")) { //快递是未取件
                    return true; //存入流中
                } else {
                    return false;
                }
            }).sorted((o1, o2) -> {
                //long o1Time = o1.getInTime().getTime();
                //long o2Time = o2.getInTime().getTime();
                //return (int)(o1Time-o2Time);
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });
            //遍历集合，将所有已取件快递存入流中
            Stream<BootStrapTableExpress> status1Express = list1.stream().filter(express -> {
                if (express.getStatus() .equals("已取件")) { //快递是已取件
                    return true; //存入流中
                } else {
                    return false;
                }
            }).sorted((o1, o2) -> {
                //long o1Time = o1.getInTime().getTime();
                //long o2Time = o2.getInTime().getTime();
                //return (int)(o1Time-o2Time);
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });
            //将流转成数组
            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            //将数组存入map中
            Map map=new HashMap();
            map.put("status0",s0);
            map.put("status1",s1);
            //填充消息对象
            msg.setData(map);
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("快递信息:"+json);
        return json;
        //测试:需要先在微信端登录
        //http://localhost:8080/wx/findExpressByUserPhone.do
    }

    /**
     * 微信端扫码时，查询二维码中封装的手机号对应的所有未取件
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/userExpressList.do")
    public String expressList(HttpServletRequest request, HttpServletResponse response){
        //接收参数
        String userPhone = request.getParameter("userPhone");
        //调用service查询
        List<Express> list = ExpressService.findByUserPhoneAndStatus(userPhone, 0);//status:0为未取件
        ////修改快递的出库时间、快递状态以及取件码的形态 因为要在页面更人性化显示
        ArrayList<BootStrapTableExpress> list1 = new ArrayList<>();
        for (Express e : list) {
            String inTime = DateFormatUtil.format(e.getInTime());//将TimeStamp类型转成string
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status=e.getStatus()==0?"待取件":"已取件";
            String code=e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list1.add(e2);
        }
        //填充消息对象
        Message msg = new Message();
        if (list.size()==0){
            msg.setStatus(-1);//未查询到指定快递
            msg.setResult("未查询到快递");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(list1); //将查询列表存入消息对象
        }
        //消息对象转json
        String json = JSONUtil.toJSON(msg);
        return json;
    }


}
